package com.ms.game.itembox.container;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.itembox.Item;
import com.ms.game.itembox.NameChangeWay;
import com.ms.game.itembox.db.ChangeNameLogModel;
import com.ms.game.itembox.message.SUseChangeNameCardResponse;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.UpdateAttributeList;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.agent.PlayerData;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.message.MessageAction;

public class ItemChangeNameCard extends ItemBasic{
	
	public static final Log log = LogFactory.getLog(ItemChangeNameCard.class);

	@Override
	public int use(Player player, Item useItem, int useCount, GUID targetGuid, Object... object) {
		return 0;
	}

	@Override
	public int applyItemToDc(BasicZoneModule zoneModule, Player player, Item useItem, GUID orderId, Object... object) {
		return 0;
	}
	
	
	/**
	 * 回傳使用更名卡的回應給Client
	 * @param sessionId
	 * @param errorCode
	 * @param name 要改的名稱
	 *            
	 */
	public static void sendResponseToClient(GUID sessionId, int errorCode, String name){
		SUseChangeNameCardResponse response = new SUseChangeNameCardResponse();
		response.setErrorCode(errorCode);
		response.setName(name);
		MessageAction.sendMessage(sessionId, MessageTypes.S_ZONE_USE_CHANGE_NAME_CARD_RESPONSE, response);
	}
	
	
	/**
	 * 扣掉更名卡回來
	 */
	public void onSubItemChangeNameCardBack(BaseDataInfo obj, Object... object){
		
		GUID orderId =(GUID)object[0];
		GUID playerId = (GUID)object[1];
		String name = (String) object[2];
		
		BasicZoneModule module = (BasicZoneModule)Contexts.getModule();
		Player player = module.getZone().getPlayer(playerId);
		
		if(player == null) {
			return;
		}
		
		if(obj.getResultCode() != ErrorCode.SUCCESS) { //DC 失敗
			if(log.isInfoEnabled()){
				log.info("Player : " + player.getGuid() + " Sub Change Name Card Fail!");
			}
			module.getZone().removeBlockList(playerId);
			ItemChangeNameCard.sendResponseToClient(player.getSessionID(), ErrorCode.DB_OPERATION_FAIL, null);
			return;
		}
		
		UpdateAttributeList updateAttrList = new UpdateAttributeList();
		updateAttrList.addUpdateValue(PlayerAttributeType.NAME, name);
		updateAttrList.addUpdateValue(PlayerAttributeType.NAME_MODIFY_TIME, player.getNameModifyTime());
		updateAttrList.addUpdateValue(PlayerAttributeType.NAME_NUMBER, player.getNameNumber());
		module.getPlayerDataAgent().updatePlayerDataToDC2(player, updateAttrList, this,"onChangeNameBack", orderId, playerId, name);
	}
	
	/**
	 * 去DC改名回來
	 * @param obj
	 * @param object
	 */
	public void onChangeNameBack(BaseDataInfo obj, Object... object) {
		
		GUID orderId = (GUID)object[0];
		GUID playerId = (GUID)object[1];
		String newName = (String)object[2];
		
		BasicZoneModule module = (BasicZoneModule)Contexts.getModule();
		Player player = module.getZone().getPlayer(playerId);
		if(player == null){
			return;
		}
		
		//移除blockList
		module.getZone().removeBlockList(playerId);

		PlayerData playerData = (PlayerData)obj;
		if(playerData.getResultCode() != ErrorCode.SUCCESS){
			if(log.isInfoEnabled()){
				log.info("Player : " + playerId + " Chagne Name Fail!");
			}
			ItemChangeNameCard.sendResponseToClient(player.getSessionID(), ErrorCode.DB_OPERATION_FAIL, null);
			return;
		}
		
		String oldName = player.getName();
		//更新回zone
		player.setName(newName); 
		
		UpdateAttributeList updateAttributeList = playerData.getUpdateAttrList();
		updateAttributeList.sendToClient(playerId, player.getSessionID(), player.getUserType());
		
		ItemChangeNameCard.sendResponseToClient(player.getSessionID(), ErrorCode.SUCCESS, newName);
		
		//寫Log
		ChangeNameLogModel model = new ChangeNameLogModel();
		model.setOrderId(orderId.toString());
		model.setPlayerId(player.getGuid().toString());
		model.setChangeBefore(oldName);
		model.setChangeAfter(newName);
		model.setChangeWay(NameChangeWay.USE_ITEM.getId());
		model.setChangeWayDesc(NameChangeWay.USE_ITEM);
		model.setZoneId(player.getZoneId());
		
		module.saveDbLog(model);
	}


}
